package com.applink.sell_proof;

import java.util.Observer;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.KeyEvent;
import android.widget.CompoundButton;

import com.actionbarsherlock.view.MenuItem;
import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.database.table.ProductImageDBHelper;
import com.applink.sell_proof.fragment.AllClaimFragment;
import com.applink.sell_proof.fragment.SearchProject;
import com.applink.sell_proof.fragment.adapter.BaseFragmentPagerAdapter;
import com.applink.sell_proof.fragment.adapter.ClaimFragmentPagerAdapter;
import com.applink.sell_proof.util.Utility;

public class ClaimActivity extends BasePagerActivity {

	protected BaseFragmentPagerAdapter mFragmentAdapter;

	public static int LOADER_DRAFT_ID = 0;
	public static int LOADER_SUBMITTED_ID = 1;
	public static int LOADER_VALIDATED_ID = 2;

	@Override
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setTitle(getString(R.string.claim));

		addSimpleOnPageChangeListener(mOnPageChangeListener);
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				new loadData().execute();
			}
		}, 5000);
	}

	@Override
	protected void onResume() {
		super.onResume();
		int checkState = Utility.checkTimeValidation();
		switch (checkState) {
		case 0:
			Utility.showAlertDialogLogout(this,
					android.R.drawable.ic_dialog_alert,
					Utility.messageServerTimeOut, Utility.titleServerTimeOut,
					ClaimActivity.this);
			break;
		case 1:
			Utility.showAlertDialogLogout(this,
					android.R.drawable.ic_dialog_alert,
					Utility.messageLocalTimeOut, Utility.titleLocalTimeOut,
					ClaimActivity.this);
			break;

		default:
			break;
		}
	}

	@Override
	protected BaseFragmentPagerAdapter getFragmentPagerAdapter() {
		if (mFragmentAdapter == null) {
			mFragmentAdapter = new ClaimFragmentPagerAdapter(
					getSupportFragmentManager());
		}
		return mFragmentAdapter;
	}

	@Override
	protected int getDefaultPagerIndex() {
		return 0;
	}

	@Override
	public void setSelectedTab() {
		((CompoundButton) findViewById(R.id.deal_tab)).setChecked(true);
	}

	@Override
	public int getActivityIndex() {
		return BasePagerActivity.SELL_ACTIVITY;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.search_bar) {

			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Fragment searchResult = getSupportFragmentManager()
					.findFragmentByTag(SearchProject.TAG);
			if (searchResult != null && searchResult.isVisible()) {
				FragmentTransaction transaction = getSupportFragmentManager()
						.beginTransaction();
				transaction.remove(searchResult);
				transaction.commit();
				return true;
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	private SimpleOnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {

		public void onPageSelected(int position) {
			try {
				DataHelper.getInstance().addObserver(
						(Observer) getFragmentAt(position));

				((AllClaimFragment) getFragmentAt(position)).getClaim();
			} catch (Exception e) {

			}
		}
	};

	private class loadData extends AsyncTask<Void, Void, Integer> {

		@Override
		protected Integer doInBackground(Void... params) {

			// Hien Nguyen Le: prevent auto sync promotion. Just getPromotion
			// when use need.
			// if (PriceBookDBHelper.getAllPriceBook() == null
			// || PriceBookDBHelper.getAllPriceBook().size() == 0) {
			//
			// DataHelper.getInstance().getPriceBook();
			// }

			// if (PromotionDBHelper.getAllPromotion() == null
			// || PromotionDBHelper.getAllPromotion().size() == 0) {
			// DataHelper.getInstance().getPromotion();
			// }

			if (ProductImageDBHelper.getAllProductImage() == null
					|| ProductImageDBHelper.getAllProductImage().size() == 0) {

				DataHelper.getInstance().getProductImage();
			}

			return 0;
		}
	}
}
